// pages/home/home.js
const app = getApp()
const net = app.globalData.net;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    city:'南宁',
    image: 'https://fakeimg.pl/24x32/?text=IMAGE',
    show: 'show',
    hidden: 'hidden',
    z_pay: 2000,
    homes:[
      
    ]
  },
  todetails(event) {
    let hid = event.currentTarget.dataset.hid;
    console.log(hid)
    wx.navigateTo({
      url: `../home/hdetails/hdetails?hid=${hid}`,
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    let _homes;
    wx.request({
      url: net+'/house/list',
      header: {
        'content-type': 'application/json' // 默认值
      },
      data:{
       
      },
      success: (res) => {
        _homes = res.data.data;
        this.setData({
          homes: _homes
        })
        console.log(_homes[0])
        
        const length = this.data.homes.length;
        for (let v of this.data.homes){
            if(v.h_img){
              v.cover = v.h_img.split(",")[0]
            }
            if(!v.you_can && v.rooms[0]){
              const payarr = v.rooms;
              console.log('房间信息',payarr)
              const _miniPrice = 0;
              let _a = [];
              for (let m of payarr) {
                _a.push(m.pay)
              }
              console.log('所有租金',_a)
              v.s_pay = Math.min(..._a)
              console.log('最低租金:',v.s_pay)
            }
        }
        for (let i = 0; i < length; ++i) {
          const x = Math.floor(Math.random() * length)
          const y = Math.floor(Math.random() * length)
          const temp = this.data.homes[x]
          this.data.homes[x] = this.data.homes[y]
          this.data.homes[y] = temp
        }
        console.log(this.data.homes)
        this.setData({
          homes: this.data.homes,
          // image: this.data.homes.h_img
        })
        
      },
      method: 'POST',
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})